//CalculatorExpression.java
//Timothy Grant
//CS1410
//Calculator 1410
//Final Version

package calculatorio;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JFileChooser;

public final class CalculatorExpression implements Serializable {

    static final long serialVersionUID = 42L;
    private String expression;
    private String expName;
    private String expType;

    public CalculatorExpression(String exp, String name, String type) {
        expression = exp;
        expName = name;
        expType = type;
    }

    public CalculatorExpression(String exp) {
        setExpression(exp);
    }

    public void setExpression(String exp) {
        expression = exp;
    }

    public boolean save2File() throws IOException {
        int buttonClicked;
        File chosenFile;
        String fileName;
        String filePath;
        ObjectOutputStream output;

        JFileChooser fileChooser = new JFileChooser();

        buttonClicked = fileChooser.showSaveDialog(null);

        if (buttonClicked != JFileChooser.APPROVE_OPTION) {
            return false;
        }

        chosenFile = fileChooser.getSelectedFile();

        fileName = chosenFile.getName();
        filePath = chosenFile.getPath();

        try {
            output = new ObjectOutputStream(new FileOutputStream(filePath));
            output.writeObject(this);
            output.close();
        } catch (IOException ex) {
            throw new IOException("Error writing\n" + filePath + "\n to disk");
        }

        return true;
    }

    public static CalculatorExpression loadFromFile() throws IOException {
        CalculatorExpression ceObject;
        JFileChooser fileChooser;
        ObjectInputStream input;
        int buttonClicked;
        File chosenFile;
        String filePath;

        fileChooser = new JFileChooser();
        buttonClicked = fileChooser.showOpenDialog(null);
        if (buttonClicked != JFileChooser.APPROVE_OPTION) {
            return null;
        }
        chosenFile = fileChooser.getSelectedFile();
        filePath = chosenFile.getPath();
        try {
            input = new ObjectInputStream(new FileInputStream(filePath));
            ceObject = (CalculatorExpression) input.readObject();
            input.close();
        } catch (EOFException e) {
            throw new IOException("Empty File");
        } catch (ClassNotFoundException ex) {
            throw new IOException("Not a Calculator Expression File");
        } catch (IOException exc) {
            throw new IOException("Error writing\n" + filePath + "\ntoDisk");
        }

        return ceObject;
    }

    public String getExpName() {
        return expName;
    }

    public String getExpType() {
        return expType;
    }

    @Override
    public String toString() {
        return expression;
    }

    String getExpression() {
        return expression;
    }
}
